package com.xky.ui.module.supplierManagement;

import com.xky.pojo.Supplier;
import com.xky.ui.main.MainView;
import com.xky.ui.module.supplierManagement.select.SelectSupplier;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.util.List;
import java.util.concurrent.ExecutionException;

/**
 * 供应商信息管理类
 * 负责显示和刷新供应商信息
 */
public class SupplierInfo {

    private static JTable supplierTable;
    private static final String MODULE_NAME = "供应商信息";

    /**
     * 显示供应商信息页面
     */
    public static void showSupplierInfoPage() {
        try {
            // 创建供应商信息面板
            JPanel supplierInfoPanel = SupplierMassage.createWarehouseInfoPanel(SupplierInfo::refreshSupplierData);
            
            // 获取表格组件
            Component centerComponent = null;
            if (supplierInfoPanel.getLayout() instanceof BorderLayout) {
                // 遍历所有组件查找位于CENTER位置的组件
                for (Component comp : supplierInfoPanel.getComponents()) {
                    Object constraints = ((BorderLayout)supplierInfoPanel.getLayout())
                            .getConstraints(comp);
                    if (BorderLayout.CENTER.equals(constraints)) {
                        centerComponent = comp;
                        break;
                    }
                }
                
                // 如果找不到，尝试直接查找JScrollPane
                if (centerComponent == null) {
                    for (Component comp : supplierInfoPanel.getComponents()) {
                        if (comp instanceof JScrollPane) {
                            centerComponent = comp;
                            break;
                        }
                    }
                }
            }
                
            if (centerComponent instanceof JScrollPane) {
                JScrollPane scrollPane = (JScrollPane) centerComponent;
                Component viewComponent = scrollPane.getViewport().getView();
                if (viewComponent instanceof JTable) {
                    supplierTable = (JTable) viewComponent;
                } else {
                    throw new IllegalStateException("无法找到表格组件: 视图组件不是表格");
                }
            } else {
                throw new IllegalStateException("无法找到表格组件: 中心组件不是滚动面板");
            }

            // 更新右侧面板
            MainView.rightPanel.removeAll();
            MainView.rightPanel.add(supplierInfoPanel, BorderLayout.CENTER);
            MainView.rightPanel.revalidate();
            MainView.rightPanel.repaint();
            
            // 注册刷新操作
            MainView.registerRefreshAction(MODULE_NAME, SupplierInfo::refreshSupplierData);
            
            // 立即刷新一次
            refreshSupplierData();
            
            // 更新状态消息
            MainView.updateStatusMessage(MODULE_NAME + "页面已加载");
        } catch (Exception e) {
            handleException(e, "加载" + MODULE_NAME + "页面失败");
        }
    }

    /**
     * 刷新供应商数据
     */
    static void refreshSupplierData() {
        // 显示加载指示器
        MainView.updateStatusMessage("正在加载" + MODULE_NAME + "数据...");
        
        new SwingWorker<List<Supplier>, Void>() {
            @Override
            protected List<Supplier> doInBackground() {
                validateTable();
                
                DefaultTableModel model = (DefaultTableModel) supplierTable.getModel();
                model.setRowCount(0);

                // 获取所有供应商数据
                return SelectSupplier.selectAll();
            }

            @Override
            protected void done() {
                try {
                    List<Supplier> supplierList = get();
                    if (supplierTable != null) {
                        SupplierMassage.updateTableData(supplierTable, supplierList);
                        MainView.rightPanel.repaint();
                        MainView.updateStatusMessage("已加载 " + supplierList.size() + " 条" + MODULE_NAME + "数据");
                    }
                } catch (InterruptedException | ExecutionException e) {
                    handleException(e, "刷新失败");
                }
            }
        }.execute();
    }
    
    /**
     * 验证表格是否已初始化
     * @throws IllegalStateException 如果表格未初始化
     */
    private static void validateTable() throws IllegalStateException {
        if (supplierTable == null) {
            throw new IllegalStateException("表格未初始化");
        }
    }
    
    /**
     * 处理异常
     * @param e 异常
     * @param message 错误消息前缀
     */
    private static void handleException(Exception e, String message) {
        e.printStackTrace();
        JOptionPane.showMessageDialog(null, 
            message + ": " + e.getMessage(), 
            "错误", JOptionPane.ERROR_MESSAGE);
        MainView.updateStatusMessage(MODULE_NAME + "数据加载失败");
    }
}
